package com.android.androidbigwork.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import androidx.fragment.app.Fragment;
import com.android.androidbigwork.R;
import com.bumptech.glide.Glide;

import java.util.List;


public class GridAdapter extends BaseAdapter {
    private Fragment fragment;
    private LayoutInflater listContainer;
    private int selectedPosition = -1;
    //本地上传
    private List<Bitmap> bmps;

    //服务器获取
    private List<String> imgList;

    public void setImgList(List<String> list){
        this.imgList = list;
    }

    //在分享页面展示 + 图片
     private boolean flag = true;

     public void setFlag(boolean f){
         flag = f;
     }

    public void setFragment(Fragment fragment) {
        this.fragment = fragment;
    }

    private class ViewHolder {
        public ImageView image;
    }

   public GridAdapter(Context context, List<Bitmap> bmp) {
        listContainer = LayoutInflater.from(context);
        bmps=bmp;
    }

    public int getCount() {
         if(bmps!=null)
           return bmps.size() + 1;
         else
             return imgList.size();
    }

    public Object getItem(int arg0) {
        return null;
    }

    public long getItemId(int arg0) {
        return 0;
    }

    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }

    public int getSelectedPosition() {
        return selectedPosition;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = listContainer.inflate(
                    R.layout.item_published_grida, null);
//            if(flag)
               holder.image = convertView.findViewById(R.id.item_grida_image);

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }


             if (bmps!=null&&position == bmps.size()&&flag) {
                 Log.d("", "getView: bmps不等于空");
                  holder.image.setImageBitmap(BitmapFactory.decodeResource(
                        fragment.getResources(), R.drawable.add_photo));
                        if (position == 10) {
                              Log.d("", "getView: 位置为10");
                               holder.image.setVisibility(View.GONE);
                        }
        }
        else if(flag){
            Log.d("", "getView: 分享页");
            holder.image.setImageBitmap(bmps.get(position));
        }
        else {
            String baseUrL = "http://39.108.183.17:8080/images/";
            for(String url:imgList){
//                Log.d("请求路径：", "网格适配器: "+baseUrL+url);
                Glide.with(fragment.getContext()).load(baseUrL+url).into(holder.image);

            }
        }
//        if(flag)
//             Log.d("", "getView: 网格分布测试");
        return convertView;
    }
}
